<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>实验三：语法分析</title>
	<link href="" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="./jquery-1.2.1.pack.js"></script>
	<script>
		$(function(){
			$("#button").click(function(){
				var string = $("#input").val();//获取输入
				if(test2(string)){//判断是否有错误
					alert("有错误");
				}else{
					test3(string);
				}
			});
		});
		function test3(s){//语法分析
			var bz = "";
			var flag = 0;
			var n = 0;
			var m = 1;
			var string = new Array();
			if(s.search(/\(|\)/)>=0){
			}else{
				for(var i=0;i<s.length;i++){
					string[n] = s[i];
					if(string[n]=="*" || string[n]=="/"){
						bz = m+": "+string[n-1]+string[n]+s[i+1]+"\n"+bz;
						var t = m+":";
						string[n-1] = t;
						i++;
						string[n] = null;
						n = n-1; 
						m++;
					}
					n++;
				}
				for(var i=0;i<string.length;i++){
					bz = ;
				}
			}
			bz = string+"\n"+bz;
			$("#bz").val(bz);
		}
		function test2(s){//对表达式进行错误判断
			var error = "";
			s = s.replace(/\d+/g,2);
			s = s.replace(/\(/g,3);
			s = s.replace(/\)/g,4);
			s = s.replace(/\+|\-|\*|\//g,1);
			var s1 = s;
			if(s1.search(/31/)>=0){
				error = "左括号后面不能存在符号\n"+error;
			}
			if(s1.search(/1$/)>=0){
				error = "不能以运算符结尾\n"+error;
			}
			if(s1.search(/^1/)>=0){
				error = "不能以运算符开头\n"+error;
			}
			if(s1.search(/14/)>=0){
				error = "右括号前面不能存在符号\n"+error;
			}
			if(s1.search(/11/)>=0){
				error = "有两个连续的运算符号\n"+error;
			}
			if(s1.search(/34/)>=0){
				error = "括号里面没有值\n"+error;
			}
			if(s1.search(/3|4/)>=0){
				var l = s1.match(/3/g);
				var r = s1.match(/4/g);
				if(r==null || l==null){
					error = "左右括号不相同\n"+error;
				}else if(l.length != r.length){
					error = "左右括号不相同\n"+error;
				}
			}
			var s2 = s1.replace(/1|2/g,'');
			for(var i=0;i<l;i++){
				s2 = s2.replace(/34/g,'');
			}
			if(s2.replace(/3|4/g)){
				error = "括号匹配错误\n"+error;
			}
			$("#error").val(error);
			if(error == ""){
				return false;
			}
			return true;
		}
	</script>
</head>
<body align="center">
	输入框：<input id="input"><br />
	输出框：<input id="text"><br />
	<input type="button" value="确定" id="button"><br />
	错误框：<textarea id="error" rows="8"></textarea><br />
	步骤框：<textarea id="bz" rows="8"></textarea>
</body>
</html>